package com.marvels.approve.loan.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.marvels.approve.common.dao.McamLoanApplyInfoCommonDao;
import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.common.dto.SynLoanStatus2ApplyDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;



/**
 * 其他子系统修改审批进件表状态
 * @author tangpw
 *
 */
@Service("loanApplyStatusModifyReceiver")
public class LoanApplyStatusModifyReceiver{
	
	@Autowired
	private McamLoanApplyInfoCommonDao mcamLoanApplyInfoCommonDao;
	@Autowired
	private IMcamLoanApplyInfoDao mcamLoanApplyInfoDao;
	
	public void onMessage(String jsonData){
		String loanId="";
		try {
	        MarvelsLogUtil.getInstance().info(">>>>>>LoanApplyStatusModifyReceiver msgJson1:" + jsonData);
            //将 "{}"替换成{}
            if (jsonData.contains("\"{")) {
            	jsonData=jsonData.substring(1, jsonData.length());
        	}
            if (jsonData.contains("}\"")) {
            	jsonData=jsonData.substring(0,jsonData.length()-1);
        	}
            if(jsonData.contains("\\")){
            	jsonData=jsonData.replaceAll("\\\\", "");
            }
            MarvelsLogUtil.getInstance().info("LoanApplyStatusModifyReceiver.onMessage msgJson2:"+jsonData);
			SynLoanStatus2ApplyDTO applyDTO=JSON.parseObject(jsonData, SynLoanStatus2ApplyDTO.class);
			String originalState=applyDTO.getOriginalState();
			loanId=applyDTO.getLoanId();
			String status=applyDTO.getStatus();
			//如果送入的originalState为空，取数据库查询状态
			McamLoanApplyInfoDTO loanApply = mcamLoanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
			if(org.apache.commons.lang.StringUtils.isEmpty(originalState)) {
				originalState = loanApply.getStatus();
			}
			//如果为送修改人默认去缓存中拿
			UserDTO userDTO= null;
			try {
				userDTO=CacheUtils.getCacheUser();
			} catch (Exception e) {
			}
			String updatedBy="";
			if(userDTO==null){
				updatedBy="system";
			}else{
				updatedBy=userDTO.getUserCode();
			}
			MarvelsLogUtil.getInstance().info(">>>>>loanId:"+loanId+",修改进件状态开始");
			int count = mcamLoanApplyInfoCommonDao.updateMcamLoanApplyInfoStatusByLoanId(loanId, status, updatedBy);
			//ack通知MQ处理成功
			MarvelsLogUtil.getInstance().info(">>>>>loanId:"+loanId+",修改进件状态结束");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>loanId:"+loanId+",修改进件状态异常",e);
		}
	}

}
